package com.xes.attend.scheduler.impl;


import com.xes.attend.config.ServerConfig;
import com.xes.attend.domain.AttendResult;
import com.xes.attend.domain.BaseException;
import com.xes.attend.scheduler.Scheduler;
import com.xes.attend.service.AttendDataService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;


/***
 * @description 下载动态考勤数据调度任务
 * @author wjl
 * @ClassName: DownloadAttendSchedulerService
 * @Version 版本
 * @ModifiedBy wjl
 * @Copyright ers
 * @date 2015年11月26日 上午11:46:05
 */
@Service
public class DownloadDynamicAttendByTodaySchedulerService implements Scheduler {
    private static final Logger log = LogManager.getLogger(DownloadDynamicAttendByTodaySchedulerService.class);
    @Autowired
    private ServerConfig config;
    @Autowired
    private AttendDataService attendDataService;

    @Override
    @Scheduled(fixedDelay = 1000 * 60 * 3)        // 3 minuts
    public void execute() {
        // 未完成预考勤下载，暂时不下载动态考勤数据
        if (!config.getDownloadAttendByToday()) {
            return;
        }
        StopWatch stopWatch = new StopWatch("动态考勤数据下载服务VenueId：" + config.getVenueId());
        stopWatch.start("下载");
        AttendResult attendDataMap = null;
        try {
            attendDataMap = attendDataService.downloadAttendDynamicDataByToday(config.getVenueId());
        } catch (BaseException e) {
            log.error("动态考勤数据下载失败:{}:{},{}", e.getResultCode(), e.getResultCode(), e.getMessage());
        }
        log.info("动态考勤数据今日共计下载:{}条", attendDataMap.getDataCount());
        stopWatch.stop();
		log.debug(stopWatch.prettyPrint());
    }

}
